import java.util.Arrays;
import java.util.PriorityQueue;

public class Test {

    public static void main(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = { 27,15,19,18,28,34,65,49,25,37 };
        testHeap.createHeap(array);

        testHeap.heapSort();
        System.out.println("aaaa");
    }

    //前k个最小的元素
    public static int[] topK(int[] array, int k) {
        //IntCmp intCmp = new IntCmp();
        PriorityQueue<Integer> priorityQueue = new PriorityQueue<>();

        for (int i = 0; i < array.length; i++) {
            priorityQueue.offer(array[i]);
        }
        //小堆创建完毕
        int[] ret = new int[k];
        for (int i = 0; i < k; i++) {
            ret[i] = priorityQueue.poll();
        }
        return ret;
    }

    //前k个最大的元素
    //时间复杂度：n*logK
//    public static int[] topK (int[] array, int k) {
//        PriorityQueue<Integer> maxHeap = new PriorityQueue<>();
//
//        for (int i = 0; i < array.length; i++) {
//            if (maxHeap.size() < k) {
//                maxHeap.offer(array[i]);
//            } else {
//                int top = maxHeap.peek();
//                if (top < array[i]) {
//                    maxHeap.poll();
//                    maxHeap.offer(array[i]);
//                }
//            }
//        }
//        int[] ret = new int[k];
//        for (int i = 0; i < k; i++) {
//            ret[i] = maxHeap.poll();
//        }
//        return ret;
//    }

    public static void main2(String[] args) {

        int[] array = {10, 4, 20, 19, 45, 80, 8, 23, 56};
        int[] ret = topK(array, 3);
        System.out.println(Arrays.toString(ret));

    }

    public static void main1(String[] args) {
        TestHeap testHeap = new TestHeap();
        int[] array = { 27,15,19,18,28,34,65,49,25,37 };
        testHeap.createHeap(array);
        testHeap.pollHeap();
        System.out.println("as");
    }

}
